package com.wonders.module.auth.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wonders.common.constant.CommonConstant;
import com.wonders.common.constant.TipConstant;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.exception.BusinessException;
import com.wonders.framework.util.ValidateUtil;
import com.wonders.framework.util.bean.BeanUtil;
import com.wonders.module.auth.constant.UserConstant;
import com.wonders.module.auth.dao.MenuDao;
import com.wonders.module.auth.dao.UserDao;
import com.wonders.module.auth.dao.UserRoleDao;
import com.wonders.module.auth.entity.bo.User;
import com.wonders.module.auth.entity.vo.UserVo;
import com.wonders.module.auth.service.UserService;

/**
 * 用户业务层实现
 * 
 * @author WuBin
 * 
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Autowired
	private MenuDao menuDao;

	@Autowired
	private UserRoleDao userRoleDao;

	public Page query(UserVo userVo, Page page) {
		page = userDao.query(userVo, page);
		return BeanUtil.copyPage(page, UserVo.class);
	}

	public void save(UserVo userVo) {
		validate(userVo, CommonConstant.Action.ADD);
		User user = BeanUtil.copyProperties(userVo, User.class);
		user.setPassword(DigestUtils.md5Hex(UserConstant.DEFAULT_PASSWORD));
		user.setCreateDate(new Date());
		user.setEnable(CommonConstant.Status.ENABLED);
		userDao.save(user);
	}

	public UserVo load(Long id) {
		User user = userDao.get(User.class, id);
		if (user == null) {
			throw new BusinessException(TipConstant.DATA.NOT_FOUND);
		}
		return BeanUtil.copyProperties(user, UserVo.class);
	}

	public void update(UserVo userVo) {
		validate(userVo, CommonConstant.Action.EDIT);
		User user = userDao.get(User.class, userVo.getId());
		if (user == null) {
			throw new BusinessException(TipConstant.DATA.NOT_FOUND);
		}
		BeanUtil.copyProperties(userVo, user, "password", "createDate",
				"enable");
		userDao.update(user);
	}

	public void remove(List<Long> keys) {
		userDao.remove(keys);
		userRoleDao.removeUserRoleByUsers(keys);
	}

	public void resetPassword(List<Long> keys) {
		userDao.editPassword(keys,
				DigestUtils.md5Hex(UserConstant.DEFAULT_PASSWORD));
	}

	public void validate(UserVo userVo, String action) {
		if (!ValidateUtil
				.nullCheck(userVo, "loginName", "name", "tel", "email")) {
			throw new BusinessException(TipConstant.PARAM.MISSING);
		}
		if (CommonConstant.Action.EDIT.equals(action)
				&& !ValidateUtil.nullCheck(userVo, "id")) {
			throw new BusinessException(TipConstant.PARAM.MISSING);
		}
		if (!ValidateUtil.isLetterAndNumber(userVo.getLoginName())
				|| !ValidateUtil.lengthCheck(userVo.getLoginName(), 3, 10)) {
			throw new BusinessException("登录名" + TipConstant.PARAM.FORMAT_ERROR);
		}
		if (!ValidateUtil.isChinese(userVo.getName())
				|| !ValidateUtil.maxLengthCheck(userVo.getName(), 10)) {
			throw new BusinessException("姓名" + TipConstant.PARAM.FORMAT_ERROR);
		}
		if (!ValidateUtil.isCellPhone(userVo.getTel())) {
			throw new BusinessException("手机号" + TipConstant.PARAM.FORMAT_ERROR);
		}
		if (!ValidateUtil.isEmail(userVo.getEmail())
				|| !ValidateUtil.maxLengthCheck(userVo.getEmail(), 50)) {
			throw new BusinessException("邮箱" + TipConstant.PARAM.FORMAT_ERROR);
		}
		List<Map<String, Object>> existList = userDao.isExist(userVo);
		if (existList != null && existList.size() > 0) {
			String key = (String) existList.get(0).get("key");
			throw new BusinessException(key + TipConstant.DATA.EXIST);
		}
	}

}
